# @author: uuz
# encoding = utf-8

from flask import Blueprint, jsonify
from flask_restful import Api, Resource, reqparse, abort
from sqlalchemy import or_

from ext import db
from models import Room, Hotel

room_bp = Blueprint("room", __name__, url_prefix='/api/room')
api = Api(room_bp)


class AddRoomView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('hotel_id', type=int, required=True)
        self.parser.add_argument('price', type=int, required=True)
        self.parser.add_argument('type', type=str, required=True)
        self.parser.add_argument('subtype', type=str, required=False)
        self.parser.add_argument('area', type=int, required=True)
        self.parser.add_argument('bed_type', type=int, required=True)
        self.parser.add_argument('window_type', type=int, required=True)
        self.parser.add_argument('breakfast', type=int, required=True)
        self.parser.add_argument('cancelable', type=int, required=True)
        self.parser.add_argument('remains', type=int, required=True)

    def post(self):
        args = self.parser.parse_args()
        hotel_id = args.get('hotel_id')
        room_price = args.get('price')
        room_type = args.get('type')
        room_subtype = args.get('subtype')
        room_area = args.get('area')
        bed_type = args.get('bed_type')
        window_type = args.get('window_type')
        breakfast = args.get('breakfast')
        cancelable = args.get('cancelable')
        room_remains = args.get('remains')

        if Hotel.query.filter(Hotel.id == hotel_id).first():
            room = Room(hotel_id=hotel_id,
                        price=room_price,
                        type=room_type,
                        subtype=room_subtype,
                        area=room_area,
                        bedtype=bed_type,
                        windowtype=window_type,
                        breakfast=breakfast,
                        cancelable=cancelable,
                        remains=room_remains)
            db.session.add(room)
            db.session.commit()
            return jsonify({"result": "OK"})
        else:
            abort(400, message="不存在该酒店id")


api.add_resource(AddRoomView, '/Room-Add/')


class DeleteRoomView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('room_id', type=int, required=True)

    def post(self):
        args = self.parser.parse_args()
        room = Room.query.filter(Room.id == args.get('room_id')).first()
        if room:
            db.session.delete(room)
            db.session.commit()
            return jsonify({"result": "删除成功"})
        else:
            abort(400, message="房间不存在")


api.add_resource(DeleteRoomView, '/Room-Delete/')


class RoomListView(Resource):

    def get(self):
        rooms = Room.query.all()
        lst = []
        for room in rooms:
            lst.append({
                "id": room.id,
                "hotel_id": room.hotel_id,
                "price": room.price,
                "type": room.type,
                "subtype": room.subtype,
                "area": room.area,
                "bed_type": room.bedtype,
                "window_type": room.windowtype,
                "breakfast": room.breakfast,
                "cancelable": room.cancelable,
                "remains": room.remains
            })
        return lst


api.add_resource(RoomListView, '/Room-List/')


class QueryRoomById(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('room_id', type=int, required=True)

    def get(self):
        room_id = self.parser.parse_args().get('room_id')
        room = Room.query.filter(Room.id == room_id).first()
        if room:
            return {
                "id": room.id,
                "hotel_id": room.hotel_id,
                "price": room.price,
                "type": room.type,
                "subtype": room.subtype,
                "area": room.area,
                "bed_type": room.bedtype,
                "window_type": room.windowtype,
                "breakfast": room.breakfast,
                "cancelable": room.cancelable,
                "remains": room.remains
            }
        else:
            abort(400, message="酒店编号不存在")


api.add_resource(QueryRoomById, '/QueryById/')


class QueryRoomByHotelId(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('hotel_id', type=int, required=True)

    def get(self):
        hotel_id = self.parser.parse_args().get('hotel_id')
        rooms = Room.query.filter(Room.hotel_id == hotel_id).all()

        if rooms:
            lst = []
            for room in rooms:
                lst.append({
                    "id": room.id,
                    "hotel_id": room.hotel_id,
                    "price": room.price,
                    "type": room.type,
                    "subtype": room.subtype,
                    "area": room.area,
                    "bed_type": room.bedtype,
                    "window_type": room.windowtype,
                    "breakfast": room.breakfast,
                    "cancelable": room.cancelable,
                    "remains": room.remains
                })
            return lst
        else:
            abort(400, message="未找到相关房间")


api.add_resource(QueryRoomByHotelId, '/QueryByHotelId/')


class QueryRoomByKeyword(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('keyword', type=str, required=True)

    def get(self):
        keyword = self.parser.parse_args().get('keyword')
        rooms = Room.query.filter(
            or_(
                Room.type.like("%" + keyword + "%"),
                Room.subtype.like("%" + keyword + "%")
            )
        ).all()

        if rooms:
            lst = []
            for room in rooms:
                lst.append({
                    "id": room.id,
                    "hotel_id": room.hotel_id,
                    "price": room.price,
                    "type": room.type,
                    "subtype": room.subtype,
                    "area": room.area,
                    "bed_type": room.bedtype,
                    "window_type": room.windowtype,
                    "breakfast": room.breakfast,
                    "cancelable": room.cancelable,
                    "remains": room.remains
                })
            return lst
        else:
            abort(400, message="未找到相关房间")


api.add_resource(QueryRoomByKeyword, '/QueryByKeyword/')